package com.jy.modules.cims.component.common.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.data.common.dao.TCMmAgreementDao;
import com.jy.modules.cims.data.common.dto.TCMmAgreementDTO;
import com.jy.platform.core.common.BaseDTO;

/**
 * @classname: TCMmAgreementService
 * @description: 定义 协议 实现类
 * @author: zhiqiang
 */
@Service("com.jy.modules.cims.component.common.service.TCMmAgreementService")
public class TCMmAgreementService implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TCMmAgreementDao dao;

	/**
	 * @author zhiqiang
	 * @description: 分页查询 协议列表
	 * @date 2015-01-05 20:59:27
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmAgreementDTO> searchTCMmAgreementByPaging(Map<String, Object> searchParams) throws Exception {
		List<TCMmAgreementDTO> dataList = dao.searchTCMmAgreementByPaging(searchParams);
		return dataList;
	}

	/**
	 * @author zhiqiang
	 * @description: 按条件查询协议列表
	 * @date 2015-01-05 20:59:27
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmAgreementDTO> searchTCMmAgreement(Map<String, Object> searchParams) throws Exception {
		List<TCMmAgreementDTO> dataList = dao.searchTCMmAgreement(searchParams);
		return dataList;
	}

	/**
	 * @author zhiqiang
	 * @description: 查询协议对象
	 * @date 2015-01-05 20:59:27
	 * @param id
	 * @return
	 * @throws
	 */
	public TCMmAgreementDTO queryTCMmAgreementByPrimaryKey(String id) throws Exception {

		TCMmAgreementDTO dto = dao.findTCMmAgreementByPrimaryKey(id);

		if (dto == null)
			dto = new TCMmAgreementDTO();

		return dto;

	}

	/**
	 * @title: insertTCMmAgreement
	 * @author zhiqiang
	 * @description: 新增 协议对象
	 * @date 2015-01-05 20:59:27
	 * @param dto
	 * @return
	 * @throws
	 */
	@SuppressWarnings("all")
	public Long insertTCMmAgreement(TCMmAgreementDTO dto) throws Exception {
		TCMmAgreementDTO hisAgreement = queryTCMmAgreementByAgreementCode(dto.getAgreementCode());
		if (null != hisAgreement) {
			throw new Exception(java.text.MessageFormat.format("协议编号[{0}]已经存在！", new Object[] { dto.getAgreementCode() }));
		}

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		int count = dao.insertTCMmAgreement(paramMap);

		TCMmAgreementDTO resultDto = (TCMmAgreementDTO) paramMap.get("dto");
		Long keyId = resultDto.getId();
		return keyId;
	}

	/**
	 * @title: updateTCMmAgreement
	 * @author zhiqiang
	 * @description: 修改 协议对象
	 * @date 2015-01-05 20:59:27
	 * @param paramMap
	 * @return
	 * @throws
	 */
	public void updateTCMmAgreement(TCMmAgreementDTO dto) throws Exception {
		TCMmAgreementDTO hisAgreement = dao.findTCMmAgreementByPrimaryKey(String.valueOf(dto.getId()));
		if (null == hisAgreement) {
			throw new Exception("原协议信息不存在！");
		}

		if (!hisAgreement.getAgreementCode().equals(dto.getAgreementCode())) {
			hisAgreement = queryTCMmAgreementByAgreementCode(dto.getAgreementCode());
			if (null != hisAgreement) {
				throw new Exception(java.text.MessageFormat.format("协议编号[{0}]已经存在！", new Object[] { dto.getAgreementCode() }));
			}
		}

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		dao.updateTCMmAgreement(paramMap);
	}

	/**
	 * @title: deleteTCMmAgreementByPrimaryKey
	 * @author zhiqiang
	 * @description: 删除 协议,按主键
	 * @date 2015-01-05 20:59:27
	 * @param paramMap
	 * @throws
	 */
	public void deleteTCMmAgreementByPrimaryKey(BaseDTO baseDto, String ids) throws Exception {
		if (StringUtils.isEmpty(ids))
			throw new Exception("删除失败！传入的参数主键为null");

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("ids", ids);
		dao.deleteTCMmAgreementByPrimaryKey(paramMap);
	}

	/**
	 * @title: queryTCMmAgreementByAgreementCode
	 * @author shengzhoutao
	 * @description: 根据协议编号查询协议信息
	 * @param agreementCode
	 *            协议编号
	 * @return TCMmAgreementDTO
	 */
	private TCMmAgreementDTO queryTCMmAgreementByAgreementCode(String agreementCode) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmAgreementDTO agreement = new TCMmAgreementDTO();
		agreement.setAgreementCode(agreementCode);
		searchParams.put("dto", agreement);
		List<TCMmAgreementDTO> agreementList = dao.searchTCMmAgreement(searchParams);

		agreement = null;
		searchParams = null;
		if (CollectionsUtils.isEmpty(agreementList)) {
			return null;
		}

		return agreementList.get(0);
	}
}
